package com.wcr.school.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcr.school.pojo.Clazz;
import com.wcr.school.pojo.Student;
import com.wcr.school.service.StudentService;
import com.wcr.school.util.MD5;
import com.wcr.school.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sms/studentController")
public class StudentController {
    @Autowired
    StudentService studentService;
    @GetMapping("/getStudentByOpr/{pageNo}/{pageSize}")
    public Result getStudentByOpr(
            @PathVariable("pageNo") Integer pageNo,
            @PathVariable("pageSize") Integer pageSize,
            Student student
    ){

//分页，带条件查询
        Page<Student> page = new Page<>(pageNo,pageSize);
        //通过服务层
        IPage<Student> iPage = studentService.getStudentByOption(page,student);
        //封装return返回
        return Result.ok(iPage);
    }

    @DeleteMapping("/delStudentById")
    public Result deleteGrade(@RequestBody List<Integer> list){
        studentService.removeByIds(list);
        return Result.ok();
    }

    @PostMapping("/addOrUpdateStudent")
    public Result addOrUpdateStudent(@RequestBody Student student){
        Integer id = student.getId();
        if(id==null||0==id){
            student.setPassword(MD5.encrypt(student.getPassword()));
        }
        studentService.saveOrUpdate(student);
        return Result.ok();
    }

}
